package se.thoka.twt.client.utils;

import com.google.gwt.core.client.GWT;

/**
 * The log levels
 * 
 * <p>
 * <b><i>Warning: The ordinal of this ENUM is used for log level, do not change!</i></b>
 * </p>
 */
public enum TWTLogLevel {
	DEBUG {
		@Override
		public native void logConsole(Object s) /*-{
					if (window.console) {
						window.console.log(s);
					}
				}-*/;
	},
	/** Trace */
	TRACE {
		@Override
		public native void logConsole(Object s) /*-{
					if (window.console) {
						window.console.log(s);
					}
				}-*/;
	},
	/** Information */
	INFO {
		@Override
		public native void logConsole(Object s) /*-{
					if (window.console) {
						window.console.info(s);
					}
				}-*/;
	},
	/** Warning */
	WARNING {
		@Override
		public native void logConsole(Object s) /*-{
					if (window.console) {
						window.console.warn(s);
					}
				}-*/;
	},
	/** Error */
	ERROR {
		@Override
		public native void logConsole(Object s) /*-{
					if (window.console) {
						window.console.error(s);
					}
				}-*/;

	};

	/**
	 * Logs an object to the console, if supported by browser
	 * 
	 * @param o The object to log
	 */
	abstract void logConsole(Object o);

	/**
	 * Logs a string and/or an exception to the hosted mode logger
	 * 
	 * @param s The string, may be <code>null</code>
	 * @param e The exception, may be <code>null</code>
	 */
	void logGWT(String s, Throwable e) {
		GWT.log(name() + ": " + s, e);
	}

	/**
	 * Logs a string and/or an exception to the system (if code is running on server)
	 * 
	 * @param s The string, may be <code>null</code>
	 * @param e The exception, may be <code>null</code>
	 */
	void logSystem(String s, Throwable e) {
		if (s != null) {
			System.out.println(name() + ": " + s);
		}
		if (e != null) {
			e.printStackTrace();
		}
	}
}